﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPaging;
using TuanAnhPersonalWeb.Classes;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin,content")]
    public class EmployeeController : Controller
    {
        private const int defaultPageSize = 10;
        private TuanAnhEntities db = new TuanAnhEntities();
        EmployeeAccessConsolidator access = new EmployeeAccessConsolidator();

        //
        // GET: /Admin/Employee/
       
        public ActionResult Index(int? page)
        {
            ViewData["Positions"] = new SelectList(access.getAllPosition(), "POS_ID", "POS_DES");
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            IList<EmployeeModel> list = access.getAllEmploy().Where(e => e.POS_ID == 1).ToList();
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListControl", list.ToPagedList(currentPageIndex, defaultPageSize));
            }
            else
            {
                return View(list.ToPagedList(currentPageIndex, defaultPageSize));
            }
        }
       
        //Search
        public ActionResult Search(int? page, string keySearch = "name", string valSearch = "", int? POS_ID = 1)
        {
            ViewData["keySearch"] = keySearch;
            ViewData["valSearch"] = valSearch;
            ViewData["Positions"] = POS_ID;
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var list = access.getAllEmploy();
            list = list.Where(e => e.EMP_NAME.ToLower().Contains(valSearch.ToLower()) && e.POS_ID == POS_ID).ToList().ToPagedList(currentPageIndex, defaultPageSize);
            if (Request.IsAjaxRequest())
            {
                return PartialView("_ListControl", list);
            }
            else
            {
                return View("Index", list);
            }

        }

        //
        // GET: /Admin/Employee/Details/5
        
        public ViewResult Details(int id)
        {
            TUANANH__EMPLOYEE tuananh__employee = db.TUANANH__EMPLOYEE.Single(t => t.EMP_ID == id);
            return View(tuananh__employee);
        }

        //
        // GET: /Admin/Employee/Create
       
        public ActionResult Create()
        {
            ViewData["Positions"] = new SelectList(access.getAllPosition(), "POS_ID", "POS_DES");
            return View();
        } 

        //
        // POST: /Admin/Employee/Create
       
        [HttpPost]
        public ActionResult Create(EmployeeModel model)
        {
            if (ModelState.IsValid)
            {
                access.InsertEmploy(model);
                return RedirectToAction("Index");  
            }
            ViewData["Positions"] = new SelectList(access.getAllPosition(), "POS_ID", "POS_DES");
            return View(model);
        }
        
        //
        // GET: /Admin/Employee/Edit/5
       
        public ActionResult Edit(int id)
        {
            EmployeeModel model = access.getEmployById(id);
            ViewData["Positions"] = new SelectList(access.getAllPosition(), "POS_ID", "POS_DES",model.POS_ID);
            return View(model);
        }

        //
        // POST: /Admin/Employee/Edit/5
       
        [HttpPost]
        public ActionResult Edit(EmployeeModel model)
        {
            if (ModelState.IsValid)
            {
                access.UpdateEmploy(model);
                return RedirectToAction("Index");
            }
            ViewData["Positions"] = new SelectList(access.getAllPosition(), "POS_ID", "POS_DES", model.POS_ID);
            return View(model);
        }

        //
        // GET: /Admin/Employee/Delete/5
        
        public ActionResult Delete(int id)
        {
            TUANANH__EMPLOYEE tuananh__employee = db.TUANANH__EMPLOYEE.Single(t => t.EMP_ID == id);
            return View(tuananh__employee);
        }

        //
        // POST: /Admin/Employee/Delete/5
        
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            TUANANH__EMPLOYEE tuananh__employee = db.TUANANH__EMPLOYEE.Single(t => t.EMP_ID == id);
            db.TUANANH__EMPLOYEE.DeleteObject(tuananh__employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}